package edu.maplef.bookwild.utils;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * this deals with the time formate problem encountered here or there
 * @author maple
 *
 */
public class DateTimeUtil {
	public DateTimeUtil() {
	}

	public static Timestamp getCurrentTimestamp() {
		Date date = new Date();
		return new Timestamp(date.getTime());
	}

	public static Timestamp dateToTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	public static String format(Timestamp timestamp, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		return sdf.format(timestamp);
	}

	public static String format(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		return sdf.format(date);
	}

	public static String dateToStr(Date date, String format) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		return sdf.format(date);
	}

	public static Date parseDate(String strDate, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		return sdf.parse(strDate);
	}

	public static Timestamp parseTimestamp(String strDate, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern(format);
		return dateToTimestamp(sdf.parse(strDate));
	}

	public static Date addDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);
		return cal.getTime();
	};

	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);
		return cal.getTime();
	}

	public static Date addMonth(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	};

	public static Date addYear(Date date, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}

	//返回两个日期相差的天数
	public static int betweenDate(String date1, String date2) {
		String[] strDt1 = StrCharUtil.split(date1, "-");
		String[] strDt2 = StrCharUtil.split(date2, "-");
		int y1, m1, d1, s1, y2, m2, d2;
		y1 = Integer.parseInt(strDt1[0].equals("") ? "0" : strDt1[0]);
		m1 = Integer.parseInt(strDt1[1].equals("") ? "0" : strDt1[1]);
		d1 = Integer.parseInt(strDt1[2].equals("") ? "0" : strDt1[2]);
		y2 = Integer.parseInt(strDt2[0].equals("") ? "0" : strDt2[0]);
		m2 = Integer.parseInt(strDt2[1].equals("") ? "0" : strDt2[1]);
		d2 = Integer.parseInt(strDt2[2].equals("") ? "0" : strDt2[2]);
		Calendar c1 = Calendar.getInstance();
		c1.set(y1, m1, d1);
		Calendar c2 = Calendar.getInstance();
		c2.set(y2, m2, d2);
		System.out.println(c1.getTimeInMillis());
		System.out.println(c2.getTimeInMillis());
		int diff = (int) (Math.abs(c1.getTimeInMillis() - c2.getTimeInMillis()) / 1000 / 60 / 60 / 24);
		int myres;
		myres = diff + 1;
		return myres;
	}

	//返回两个日期相差的小时数，取绝对值,cxlh贴加
	public static double betweenDate(String date1, String time1, String date2,
			String time2) {
		String[] strDt1 = StrCharUtil.split(date1, "-");
		String[] strTime1 = StrCharUtil.split(time1, ":");
		String[] strDt2 = StrCharUtil.split(date2, "-");
		String[] strTime2 = StrCharUtil.split(time2, ":");
		int y1, m1, d1, h1, mm1, s1, y2, m2, d2, h2, mm2, s2;
		y1 = Integer.parseInt(strDt1[0].equals("") ? "0" : strDt1[0]);
		m1 = Integer.parseInt(strDt1[1].equals("") ? "0" : strDt1[1]);
		d1 = Integer.parseInt(strDt1[2].equals("") ? "0" : strDt1[2]);
		h1 = Integer.parseInt(strTime1[0].equals("") ? "0" : strTime1[0]);
		mm1 = Integer.parseInt(strTime1[1].equals("") ? "0" : strTime1[1]);
		s1 = Integer.parseInt(strTime1[2].equals("") ? "0" : strTime1[2]);
		y2 = Integer.parseInt(strDt2[0].equals("") ? "0" : strDt2[0]);
		m2 = Integer.parseInt(strDt2[1].equals("") ? "0" : strDt2[1]);
		d2 = Integer.parseInt(strDt2[2].equals("") ? "0" : strDt2[2]);
		h2 = Integer.parseInt(strTime2[0].equals("") ? "0" : strTime2[0]);
		mm2 = Integer.parseInt(strTime2[1].equals("") ? "0" : strTime2[1]);
		s2 = Integer.parseInt(strTime2[2].equals("") ? "0" : strTime2[2]);
		Calendar c1 = Calendar.getInstance();
		c1.set(y1, m1, d1, h1, mm1, s1);
		Calendar c2 = Calendar.getInstance();
		c2.set(y2, m2, d2, h2, mm2, s2);
		double diff = Math.abs(c1.getTimeInMillis() - c2.getTimeInMillis()) / 1000 / 60 / 60.0;
		double myres;
		myres = diff;
		return myres;
	}

	public static double betweenDate(String date1, String time1, String date2,
			String time2, boolean isRound) {
		double diff = betweenDate(date1, time1, date2, time2);
		if (isRound)
			diff = Math.round(diff);
		else
			diff = Math.floor(diff);
		return diff;
	}

	public static int getStartPoint(String time1) throws Exception {
		Timestamp sdf = parseTimestamp(time1, "HH:mm");
		return Math.round(sdf.getHours() * 4
				+ Math.round(sdf.getMinutes() / 15.0));
	}

	public static int[] getPointArray(String time1, String time2)
			throws Exception {
		int start = getStartPoint(time1);
		int tmp = start + 1;
		int end = getStartPoint(time2);
		int[] res = new int[end - start + 1];
		for (int i = 0; i <= end - start; i++) {
			res[i] = tmp++;
		}
		return res;
	}

	public static String addSqlDay(String dt, int days) throws Exception {
		Date dt1 = DateTimeUtil.parseDate(dt, "yyyy-mm-dd");
		Date dtres = DateTimeUtil.addDay(dt1, days);
		String res = DateTimeUtil.dateToStr(dtres, "yyyy-mm-dd");
		return res;
	}

	public static java.sql.Date utilDate2sqlDate(java.util.Date utildate) {
		return new java.sql.Date(utildate.getTime());
	}

	public static java.util.Date sqlDate2utilDate(java.sql.Date sqldate) {
		return (java.util.Date) sqldate;
	}

	//传入yyyy-mm-dd的日期字符串返回yyyy-mm-dd 星期的形式
	public static String getWeekNumberFromDate(String thedate) {
		String strReturn = "";
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateStringToParse = thedate;
		try {
			Date date = bartDateFormat.parse(dateStringToParse);
			SimpleDateFormat bartDateFormat2 = new SimpleDateFormat(
					"yyyy-MM-dd EEEE");
			strReturn = bartDateFormat2.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strReturn;
	}

	/** 计算两个时间差，返回相差秒数 
	 * @throws Exception */
	public static long subTime(String src, String des) throws Exception {
		long n = 0, n1 = 0;
		java.util.Calendar ca = java.util.Calendar.getInstance();
		ca.setTime(parseDate(src, "yyyy-MM-dd HH:mm:ss"));
		java.util.Calendar ca1 = java.util.Calendar.getInstance();
		ca1.setTime(parseDate(des, "yyyy-MM-dd HH:mm:ss"));
		n = ca.getTimeInMillis();
		n1 = ca1.getTimeInMillis();
		return Math.abs(n - n1) / 1000;
	}

	public static int[] getHMS(long secode) {
		int[] res = new int[3];
		int h = (int) secode / 3600;
		int m = (int) ((secode / 3600.0d - h) * 60);
		int s = (int) (secode % 60);
		res[0] = h;
		res[1] = m;
		res[2] = s;
		return res;
	}

	public static String getPMAM(String dt) throws Exception {
		if (dt == null || dt.equals("") || dt.length() <= 12)
			return "";
		Date mydt = DateTimeUtil.parseDate(dt, "yyyy-MM-dd HH:mm:ss");
		String myhour = DateTimeUtil.format(mydt, "HH");
		String hm = DateTimeUtil.format(mydt, "HH:mm");
		if (!myhour.equals("")) {
			int h = Integer.parseInt(myhour);
			return hm + " " + (h >= 13 ? "PM" : "AM");
		}
		return hm;
	}

	public static String getTime(String date, String format) {
		try {
			if (format == null || format.equals(""))
				format = "yyyy-MM-dd HH:mm";
			long ll = Long.parseLong(date);
			Date dt = new Date(ll);
			return DateTimeUtil.format(dt, format);
		} catch (Exception ex) {
			return "";
		}
	}

	public static String getNow(int len) {
		try {
			long ll = new Date().getTime();
			return StrCharUtil.addZero(String.valueOf(ll), len);
		} catch (Exception ex) {
			return String.valueOf(new Date().getTime());
		}
	}

	public static Date str2Date(String time) {
		try {
			SimpleDateFormat formatter;
			int tempPos = time.indexOf("AD");
			time = time.trim();
			formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
			if (tempPos > -1) {
				time = time.substring(0, tempPos) + "公元"
						+ time.substring(tempPos + "AD".length());//china   
				formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
			}
			tempPos = time.indexOf("-");
			if (tempPos > -1 && (time.indexOf(" ") < 0)) {
				formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");
			} else if ((time.indexOf("/") > -1) && (time.indexOf(" ") > -1)) {
				formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			} else if ((time.indexOf("-") > -1) && (time.indexOf(" ") > -1)) {
				formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			} else if ((time.indexOf("/") > -1) && (time.indexOf("am") > -1)
					|| (time.indexOf("pm") > -1)) {
				formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
			} else if ((time.indexOf("-") > -1) && (time.indexOf("am") > -1)
					|| (time.indexOf("pm") > -1)) {
				formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
			} else {
				return new Date(time);
			}
			ParsePosition pos = new ParsePosition(0);
			java.util.Date ctime = formatter.parse(time, pos);

			return ctime;
		} catch (Exception ex) {
			return new Date();
		}
	}

	public static void main(String[] args) {
		/// Calendar cald=Calendar.getInstance();
		try {
			System.out.println(DateTimeUtil
					.str2Date("Mon, 16 Jun 2008 09:47:00"));
		} catch (Exception e) {
			System.out.print("error");
		}
	}
}
